package com.ruoyi.student_club.service.impl;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.student_club.domain.Club;
import com.ruoyi.student_club.domain.ClubMember;
import com.ruoyi.student_club.dto.ActivityDto;
import com.ruoyi.student_club.mapper.ClubMapper;
import com.ruoyi.student_club.vo.app.ActivityDetail;
import com.ruoyi.student_club.vo.app.ClubActivityVo;
import com.ruoyi.student_club.vo.app.ClubListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student_club.mapper.ClubActivityMapper;
import com.ruoyi.student_club.domain.ClubActivity;
import com.ruoyi.student_club.service.IClubActivityService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;

/**
 * 活动管理Service业务层处理
 * 
 * @author 无尤
 * @date 2022-01-24
 */
@Service
public class ClubActivityServiceImpl implements IClubActivityService 
{
    @Resource
    private ClubActivityMapper clubActivityMapper;

    @Resource
    private ClubMapper clubMapper;

    /**
     * 查询活动管理
     * 
     * @param id 活动管理主键
     * @return 活动管理
     */
    @Override
    public ClubActivity selectClubActivityById(Long id)
    {
        return clubActivityMapper.selectClubActivityById(id);
    }

    /**
     * 查询活动管理列表
     * 
     * @param clubActivity 活动管理
     * @return 活动管理
     */
    @Override
    public List<ClubActivity> selectClubActivityList(ClubActivity clubActivity)
    {
        Long clubId = ShiroUtils.getSysUser().getClubId();
        clubActivity.setClubId(clubId);
        return clubActivityMapper.selectClubActivityList(clubActivity);
    }

    /**
     * 新增活动管理
     * 
     * @param clubActivity 活动管理
     * @return 结果
     */
    @Override
    public int insertClubActivity(ClubActivity clubActivity) {
        clubActivity.setCreateTime(DateUtils.getNowDate());
        clubActivity.setUpdateTime(DateUtils.getNowDate());
        SysUser current = ShiroUtils.getSysUser();
        //获取社团信息
        Club club = clubMapper.getClubById(current.getClubId());
        if (club == null){
            throw new RuntimeException("此社团不存在,请联系管理员");
        }
        clubActivity.setClubId(current.getClubId());
        clubActivity.setClubName(club.getClubName());
        return clubActivityMapper.insertClubActivity(clubActivity);
    }

    /**
     * 修改活动管理
     * 
     * @param clubActivity 活动管理
     * @return 结果
     */
    @Override
    public int updateClubActivity(ClubActivity clubActivity)
    {
        clubActivity.setUpdateTime(DateUtils.getNowDate());
        return clubActivityMapper.updateClubActivity(clubActivity);
    }

    /**
     * 批量删除活动管理
     * 
     * @param ids 需要删除的活动管理主键
     * @return 结果
     */
    @Override
    public int deleteClubActivityByIds(String ids)
    {
        return clubActivityMapper.deleteClubActivityByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除活动管理信息
     * 
     * @param id 活动管理主键
     * @return 结果
     */
    @Override
    public int deleteClubActivityById(Long id)
    {
        return clubActivityMapper.deleteClubActivityById(id);
    }

    /**
     * 获取社团活动列表
     * @param clubId 社团id
     * @return
     */
    @Override
    public List<ClubActivityVo> getClubActivityList(Long clubId){
        return clubActivityMapper.getClubActivityList(clubId);
    }

    /**
     * 获取社团活动详情
     * @param id 活动id
     * @return
     */
    @Override
    public ClubActivity getActivityDetail(Long id){
        return clubActivityMapper.getActivityDetail(id);
    }

    @Override
    public PageInfo getActivityList(ActivityDto dto){
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getSize() == null){
            dto.setSize(10);
        }
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<ClubActivity> activityList = clubActivityMapper.getActivityList(dto);
        List<ClubActivityVo> activityVoList = new ArrayList<>();
        for (ClubActivity clubActivity : activityList){
            ClubActivityVo activityVo = new ClubActivityVo();
            activityVo.setId(clubActivity.getId());
            activityVo.setTitle(clubActivity.getTitle());
            //放入活动内容
            String content = replaceContent(clubActivity.getContent());
            activityVo.setContent(content);
            String avatar = clubMapper.getAvatar(clubActivity.getClubId());
            activityVo.setAvatar(avatar);
            activityVoList.add(activityVo);

        }
        PageInfo pageInfo = new PageInfo(activityVoList);
        return pageInfo;
    }


    /**
     * 获取活动详情
     * @param id 活动id
     * @return
     */
    @Override
    public ActivityDetail activityDetail(Long id){
        if (id == null || id <= 0){
            throw new RuntimeException("存在注入风险！！！");
        }
        ActivityDetail activityDetail = clubActivityMapper.activityDetail(id);
        if (activityDetail == null){
            throw new RuntimeException("无此活动");
        }
        return activityDetail;
    }

    /**
     * 替换图片地址
     * @param content 原始内容
     * @return
     */
    public String replaceContent(String content){
        String old = "http://localhost/profile";
        String target = "http://www.ec-wuyou.xyz/upload/activity_picture";
        return content.replace(old,target);
    }

}
